package com.zhuss.food.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.food.order.CancelOrderBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.ScheduleDao;
import com.zhuss.core.db.dao.gen.UserMapper;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.OrderStatus;
import com.zhuss.enums.ProductType;
import com.zhuss.enums.SystemEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.food.db.dao.ext.OrderDao;
import com.zhuss.food.db.dao.ext.OrderProductDao;
import com.zhuss.food.db.dao.ext.ProductDao;
import com.zhuss.food.db.entity.Order;
import com.zhuss.food.db.entity.OrderProduct;
import com.zhuss.util.HeaderUtil;
import com.zhuss.util.JsonUtil;
/**
 * 
 * @author zhushanshuo
 * @since 2020.03.09 13:10
 *
 */

@Component("orderCancel")
public class OrderCancel extends AbstractProcessor<Object, CancelOrderBo> implements Processor<Object, CancelOrderBo> {
	
	@Autowired
	private OrderDao orderDao;
	
	@Autowired
	private OrderProductDao orderProductDao;
	
	@Autowired
	private ProductDao productDao;
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private OrderCheck orderCheck;
	
	@Autowired
	private ScheduleDao scheduleDao;
	
	@Override
	public void check(CancelOrderBo bo) throws WpsException {
		Long orderId = bo.getOrderId();
		Long userId = bo.getConcurrentUserId();
		Integer area = bo.getConcurrentArea();
		Integer operateType = bo.getOperateType();
		
		Integer system = HeaderUtil.systemTl.get();
		if (operateType == null) {
			operateType = system;
		}
		
		// 当前订单不满足取消条件
		if (!orderCheck.checkCancel(orderId)) {
			throw new WpsException(ExceptionCode.当前订单不满足取消条件);
		}
		
		// 每个用户只能取消自己的订单
		if (SystemEnum.小程序.sameKey(operateType) && !orderCheck.checkBelongUser(orderId, userId)) {
			throw new WpsException(ExceptionCode.每个用户只能取消自己的订单);
		}
		
		// 取消时间限制
		if (SystemEnum.小程序.sameKey(operateType) && orderCheck.checkOverCancelTime(orderId, area)) {
			throw new WpsException(ExceptionCode.已超过最晚取消时间);
		}
		
	}
	
	@Override
	public Object process(CancelOrderBo bo) throws WpsException {
		
		Long orderId = bo.getOrderId();
		Integer area = bo.getConcurrentArea();
		
		
		
		Order orderDb = orderDao.selectByPrimaryKey(orderId);
		Integer type = orderDb.getType();
		if (ProductType.特价外卖.sameKey(type)) {
			List<OrderProduct> list = orderProductDao.listOrderProduct(orderId);
			OrderProduct orderProduct = list.get(0);
			Long productId = orderProduct.getProductId();
			// 释放商品库存 ，  返回受影响数量
			Integer influnce = productDao.used(productId, -1l);
			if (influnce.equals(0)) {
				throw new WpsException(ExceptionCode.特价外卖库存不足);
			}
		}
		
		if (ProductType.点炒服务.sameKey(type)) {
			Date expectFetchTimeStart = orderDb.getExpectFetchTimeStart();
			// 释放点炒取餐时间区间库存 ，  返回受影响数量
			Integer influnce = scheduleDao.used(expectFetchTimeStart, -1, area);
			if (influnce.equals(0)) {
				throw new WpsException(ExceptionCode.当前取餐时间数量已满);
			}
		}
		
		if (ProductType.公务接待.sameKey(type)) {
			// 释放包厢的用餐时间库存 ，  返回受影响数量
			Date date = orderDb.getDate();
			Integer foodTime = orderDb.getFoodTime();
			Integer influnce = scheduleDao.usedFoodTime(date, foodTime, -1, area);
			if (influnce.equals(0)) {
				throw new WpsException(ExceptionCode.包厢已满);
			}
		}
		
		Order order = new Order();
		order.setOrderId(bo.getOrderId());
		order.setModifierId(bo.getConcurrentUserId());
		order.setModifier(bo.getConcurrentUserName());
		order.setModifyTime(new Date());
		order.setStatus(OrderStatus.已取消.key());
		orderDao.updateByPrimaryKeySelective(order);
		
		orderProductDao.cancel(orderId, OrderStatus.已取消.key());
		return null;
	}

	@Override
	public Object log(CancelOrderBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.餐饮.key());
    	record.setAction(LogActionEnum.订单取消.key());
    	record.setTargetId(param.getOrderId());
    	record.setTargetName(param.getOrderId() + "");
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}


}
